home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / misc1 / iv26_w30.zip / EXAMPLES / IFB / CYCLEBUT.C next >
C/C++ Source or Header  |  1992-02-04  |  4KB  |  125 lines

  1. /*
  2.  * Copyright (c) 1989 Stanford University
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software and its
  5.  * documentation for any purpose is hereby granted without fee, provided
  6.  * that the above copyright notice appear in all copies and that both that
  7.  * copyright notice and this permission notice appear in supporting
  8.  * documentation, and that the name of Stanford not be used in advertising or
  9.  * publicity pertaining to distribution of the software without specific,
  10.  * written prior permission.  Stanford makes no representations about
  11.  * the suitability of this software for any purpose.  It is provided "as is"
  12.  * without express or implied warranty.
  13.  *
  14.  * STANFORD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  15.  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
  16.  * IN NO EVENT SHALL STANFORD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  17.  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  18.  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  19.  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  20.  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  21.  */
  22.  
  23. /*
  24.  * a Button which cycles through multiple choices
  25.  */
  26.  
  27. #include "cyclebut.h"
  28.  
  29. #include <InterViews/bitmap.h>
  30. #include <InterViews/font.h>
  31. #include <InterViews/painter.h>
  32. #include <InterViews/shape.h>
  33.  
  34. static const int cycle_width = 11;
  35. static const int cycle_height = 11;
  36. static char cycle_mask[] = {
  37.    0x78, 0x00, 0xfc, 0x05, 0xfe, 0x07, 0xfe, 0x07, 0xff, 0x07, 0xff, 0x07,
  38.    0xff, 0x07, 0xff, 0x03, 0xff, 0x03, 0xfd, 0x01, 0xf0, 0x00};
  39. static char cycle_plain[] = {
  40.    0x78, 0x00, 0x84, 0x05, 0x00, 0x07, 0x80, 0x04, 0x01, 0x05, 0x03, 0x06,
  41.    0x05, 0x04, 0x09, 0x00, 0x07, 0x00, 0x0d, 0x01, 0xf0, 0x00};
  42. static char cycle_hit[] = {
  43.    0x78, 0x00, 0x84, 0x05, 0x00, 0x07, 0x80, 0x07, 0x01, 0x07, 0x03, 0x06,
  44.    0x07, 0x04, 0x0f, 0x00, 0x07, 0x00, 0x0d, 0x01, 0xf0, 0x00};
  45.  
  46. static Bitmap* CycleMask;
  47. static Bitmap* CyclePlain;
  48. static Bitmap* CycleHit;
  49.  
  50. static const int Separator = 3;
  51.  
  52. CycleButton::CycleButton (
  53.     ButtonState* subject, CycleButtonChoice* choices
  54. )  : (subject, nil) {
  55.     Init(choices);
  56. }
  57.  
  58. CycleButton::CycleButton (
  59.     const char* name, ButtonState* subject, CycleButtonChoice* choices
  60. ) : (subject, nil) {
  61.     SetInstance(name);
  62.     Init(choices);
  63. }
  64.  
  65. CycleButton::~CycleButton () { }
  66.  
  67. void CycleButton::Init (CycleButtonChoice* c) {
  68.     SetClassName("CycleButton");
  69.     if (CycleMask == nil) {
  70.         CycleMask = new Bitmap(cycle_mask, cycle_width, cycle_height);
  71.         CyclePlain = new Bitmap(cycle_plain, cycle_width, cycle_height);
  72.         CycleHit = new Bitmap(cycle_hit, cycle_width, cycle_height);
  73.     }
  74.     choices = c;
  75.     for (count = 0; choices[count].label != nil; ++count) { }
  76.     current = 0;
  77. }
  78.  
  79. void CycleButton::Reconfig () {
  80.     Font* font = output->GetFont();
  81.     int width = 0;
  82.     for (int i = 0; i < count; ++i) {
  83.         width = max(width, font->Width(choices[i].label));
  84.     }
  85.     Button::Reconfig();
  86.     shape->Rect(width + cycle_width + Separator, font->Height());
  87.     shape->Rigid();
  88. }
  89.  
  90. void CycleButton::Redraw (Coord l, Coord b, Coord r, Coord t) {
  91.     Coord textx = cycle_width + Separator;
  92.     Coord texty = (ymax + 1 - output->GetFont()->Height()) / 2;
  93.     output->ClearRect(canvas, l, b, r, t);
  94.     output->Text(canvas, choices[current].label, textx, texty);
  95.     Refresh();
  96. }
  97.  
  98. void CycleButton::Refresh () {
  99.     Coord bitsx = 0;
  100.     Coord bitsy = (ymax + 1 - cycle_height)/2;
  101.     if (!hit) {
  102.         output->Stencil(canvas, bitsx, bitsy, CyclePlain, CycleMask);
  103.     } else {
  104.         output->Stencil(canvas, bitsx, bitsy, CycleHit, CycleMask);
  105.     }
  106. }
  107.  
  108. void CycleButton::Press () {
  109.     subject->SetValue(choices[(current + 1) % count].value);
  110. }
  111.  
  112. void CycleButton::Update () {
  113.     void* v;
  114.     subject->GetValue(v);
  115.     if (v != choices[current].value) {
  116.         for (int i = 0; i < count; ++i) {
  117.             if (v == choices[i].value) {
  118.                 current = i;
  119.                 break;
  120.             }
  121.         }
  122.         Draw();
  123.     }
  124. }
  125.